package com.other;


import net.sf.json.JSONObject;

/**
 * Created by baiguantao on 2017/6/13.
 */
public class JsonUtils2 {

    /**
     * 2017-06-09
     * 公共解析实体
     * {
     *     "key":"value",
     *     "key2":"{
     *         "innerkey":"innervalue"
     *     }"
     * }
     * msg：消息字符串
     * key：这里对应的是key2
     * classz：对应实体
     * */
    public static Object analyze(JSONObject MQjson, String key, Class classz){
        //解析
        return JSONObject.toBean(JSONObject.fromObject(MQjson.get(key)),classz);
    }

    /**
     * 获取json对象
     * @param msg
     * @return
     */
    public static JSONObject getJsonObject(String msg){
        return JSONObject.fromObject(msg);
    }

    public static void main(String[] args) {
        String ACCOUNT_OPERATE = "{subject:'1',"

                + "userkey:'%s'}";
        User user=new User();
        user.setAge(10);
        user.setName("ricky");
        user.setPwd("123466");
        //结构拼装
        String msg = String.format(ACCOUNT_OPERATE,
                JSONObject.fromObject(user));
        User user1= (User) analyze(getJsonObject(msg),"userkey",User.class);
        System.out.println(user1.getName());
    }
}
